{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AGiXT Chatbot Example\n",
    "\n",
    "This is an example of a basic AGiXT chatbot. This will allow you to turn any provider/model into a chatbot based on the data you give it with long and short term conversation history with that. This is a very basic example, but it can be expanded upon to make a more robust chatbot if you choose to have your agent learn from more documents and websites.\n",
    "\n",
    "## Install latest AGiXT SDK\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -U agixtsdk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to AGiXT and set your agent\n",
    "\n",
    "Update the `base_uri`, `api_key`, and `agent_name` variables below to match your AGiXT instance if you are not using the default values.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agixtsdk import AGiXTSDK\n",
    "\n",
    "# Your AGiXT URL and API key\n",
    "base_uri = \"http://localhost:7437\"\n",
    "api_key = None\n",
    "\n",
    "ApiClient = AGiXTSDK(base_uri=base_uri, api_key=api_key)\n",
    "\n",
    "# Your agent name with provider settings all set up.\n",
    "# We will use the built in gpt4free agent for this example.\n",
    "agent_name = \"gpt4free\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Learn from some documentation for the chatbot to use in context\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ApiClient.learn_url(\n",
    "    agent_name=agent_name, url=\"https://josh-xt.github.io/AGiXT/\", collection_number="0"\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a new Conversation and define the chat function\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "# New chatbot session\n",
    "conversation = \"New Conversation \" + str(datetime.now())\n",
    "print(f\"Conversation ID: {conversation}\")\n",
    "\n",
    "\n",
    "def chat(user_input):\n",
    "    return ApiClient.chat(\n",
    "        user_input=user_input,\n",
    "        conversation=conversation,\n",
    "        agent_name=agent_name,\n",
    "        context_results=5,  # Number of agent memories to inject into context\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat with the chatbot\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ai_response = chat(user_input=\"What is AGiXT?\")\n",
    "print(ai_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat with the chatbot again\n",
    "\n",
    "Keep using the same conversation ID to keep the history of the conversation going. Do this as many times as you want to keep the conversation going, or start a new conversation with a new UUID to start over with the same agent.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ai_response = chat(user_input=\"Awesome! What could I do with it?\")\n",
    "print(ai_response)\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
